//
//  ShoeBagDressViewController.m
//  EasyToTesco
//
//  Created by mc on 16/1/31.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ShoeBagDressViewController.h"
#import "ShoeBagCollectionViewCell.h"
//导入下拉刷新头文件
//
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
//滑动菜单

//商品详情页面
#import "ShoperDetailViewController.h"

#import "NetWorkTools.h"
#import "ShopCartViewController.h"
#import "JudgeUserLogInTool.h"

//注册cell
static NSString *cellIndentifier = @"ShoeBagCollectionViewCell";
//注册区头
//static NSString *headerIndentifier = @"HEADER";

@interface ShoeBagDressViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
 //二级id
    NSString *ffid;
    //三级id
    NSString *fid;
    //判断下拉的时候加载什么数据
    NSInteger what;
    
}

//@property (nonatomic, strong)UIView *firstView;
//@property (nonatomic, strong)UIView *secondView;
//
//@property (nonatomic, strong)UIButton *leftBtn;
//
//@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong)UICollectionView *ShoeColllectionview;
//添加刷新控件




//存放选择按钮数据的数组

@property (nonatomic, strong)NSMutableArray *WholeClassArray;

@property (nonatomic, strong)NSMutableArray *firstArray;
@property (nonatomic, strong)NSMutableArray *secondArray;

@property (nonatomic, strong)NSMutableArray *firstIdArray;
@property (nonatomic, strong)NSMutableArray *secondIdArray;

@property (nonatomic, strong)NSMutableArray *wholeGoodsArray;

@property (nonatomic,strong)NSArray *xingYingGoodsArray;

@end

@implementation ShoeBagDressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.WholeClassArray = [@[]mutableCopy];
    self.wholeGoodsArray = [@[]mutableCopy];
    
    self.firstArray = [@[]mutableCopy];
    self.secondArray = [@[]mutableCopy];
    self.firstIdArray = [@[]mutableCopy];
    self.secondIdArray = [@[]mutableCopy];
    
    what = 0;
    
    [self addshoebagDressUI];
    
    self.title = @"北大荒自然村";
    UIButton *shopCart = [[UIButton alloc] init];
    [shopCart setFrame:CGRectMake(WIDTH - 60, HEIGHT - 100, 40, 40)];
    //        [shopCart setBackgroundColor:[UIColor redColor]];
    [shopCart addTarget:self action:@selector(clickShopCart) forControlEvents:UIControlEventTouchUpInside];
    [shopCart setImage:[UIImage imageNamed:@"gouwuche"] forState:UIControlStateNormal];
    [self.view addSubview:shopCart];
}

- (void)clickShopCart {
    NSLog(@"...");
    if ([JudgeUserLogInTool JudgeUserLonInStatus]) {
        
        [self.navigationController pushViewController:[[ShopCartViewController alloc] init] animated:YES];
    }
    
}


- (void)addshoebagDressUI {
//    [self addshoeTopUI];
    
  
    
    [self addCollectionUI];
//    [self setupHeader];
//    [self setupFooter];
    
//    [self getSubClassData];
    //只有一级id的数据解析
    [self getShoesAndColorData];
    
}







- (void)addCollectionUI {
    UICollectionViewFlowLayout *layout = [[ UICollectionViewFlowLayout alloc] init];
    //滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //cell大小
    //    layout.itemSize = CGSizeMake(50, 50);
    //最小的行间距
    layout.minimumLineSpacing = 5;
    //cell之间的最小距离
    layout.minimumInteritemSpacing = 0;
    
    //设置区头大小
//        layout.headerReferenceSize = CGSizeMake(WEITH, 80);
    //    //设置区尾大小
    //    layout.footerReferenceSize = CGSizeMake(375, 50);
    //设置分区的间距, 不包含区头和区尾, 只影响内容部分的间距
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    UICollectionView *collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WEITH, HIGHT) collectionViewLayout:layout];
    collectionview.delegate = self;
    collectionview.dataSource = self;
    collectionview.backgroundColor = [UIColor whiteColor];
    self.ShoeColllectionview = collectionview;
    [self.view addSubview:self.ShoeColllectionview];
    
    
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"ShoeBagCollectionViewCell" bundle: [NSBundle mainBundle]];
    [self.ShoeColllectionview registerNib:nib forCellWithReuseIdentifier:cellIndentifier];
    
    
    

}




#pragma mark -- UICollectionViewDataSource
//某个分区的CELL或者是Item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.xingYingGoodsArray.count;
}

//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //UICollectionViewCell, 继承于UICollectionReusableView , cell上没有控件, 如果需要控件的话, 需要自定义cell
        ShoeBagCollectionViewCell *cell = (ShoeBagCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[ShoeBagCollectionViewCell alloc] init];
    }
    
    
    NSString *URLString = @"http://120.76.162.124:8083";
    
    NSString *PicString = [URLString stringByAppendingString:[self.xingYingGoodsArray[indexPath.row] objectForKey:@"PictureURL"]];

    NSLog(@"%@",PicString);
    [cell.ShoePicImage sd_setImageWithURL:[NSURL URLWithString:PicString]placeholderImage:[UIImage imageNamed:@"dress.png"]];
        NSString *string =[self.xingYingGoodsArray[indexPath.row] objectForKey:@"Title"];
    
        cell.DetailLabel.text = string;
    
        cell.SCJLabel.text = [NSString stringWithFormat:@"%@",[self.xingYingGoodsArray[indexPath.row] objectForKey:@"SellPrice"]];
    
//    [self.xingYingGoodsArray[indexPath.row] objectForKey:@""];
            cell.MoneyLabel.text = [NSString stringWithFormat:@"市场价:"];

    
        return cell;
}



#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (indexPath.row == 0) {
//        [self.xingYingGoodsArray[indexPath.row] objectForKey:@"Id"];
        
        ShoperDetailViewController *shopsDetailVC = [[ShoperDetailViewController alloc] init];
        shopsDetailVC.xingYingShopID = [self.xingYingGoodsArray[indexPath.row] objectForKey:@"Id"];
        
        [self.navigationController pushViewController:shopsDetailVC animated:YES];
        NSLog(@"%ld--%ld", indexPath.section, indexPath.row);

}

#pragma mark - UICollectionViewDelegateFlowLayout
//返回指定cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 20)/2, 260);
}

//返回某个分区的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 5, 0, 5);
}

//最小的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}
//返回某一个分区, item之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}


- (void)footerRefresh
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
#pragma mark -- 加载数据的方法
        if (what == 0) {
//            [self getSubClassData];
            [self getShoesAndColorData];
        } else if (what == 1) {
//            [self getsecondgoodsData];
        } else {
//            [self getthreegoodsData];
        }
        [self.ShoeColllectionview reloadData];

    });
}


//获取给分类下的所有商品
- (void)getShoesAndColorData{
    
    NSDictionary *dict = @{};
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"PRODUCTLISTQUERY" completeBlock:^(id object) {
        
        NSString *DataString = [object objectForKey:@"BackData"];
        
        NSDictionary *dict = [NetWorkTools dictionaryWithJsonString:DataString];
        
        self.xingYingGoodsArray = [dict objectForKey:@"ds"];
        
//        NSLog(@"%@",object);
//        self.xingYingGoodsArray = [NetWorkTools arrayWithJsonString:[object objectForKey:@"BackData"]];
        
        NSLog(@"%@",self.xingYingGoodsArray);
        
        [self.ShoeColllectionview reloadData];
        
//        NSDictionary *dict = array[0];
//        NSString *str = dict[@"Title"];
        
//        NSLog(@"%@",str);
    }];

}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}






@end
